//
//  MyFansVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyFansVC.h"
#import "FansCell.h"
#import "OtherInfoVC.h"
#import "MyVM.h"
#import "MJRefresh.h"
#import "MasterRankVC.h"
@interface MyFansVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,copy) NSString    *lastId;




@end

@implementation MyFansVC

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type==FansTypeDefault) {
//        UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"排行榜" style:UIBarButtonItemStylePlain target:self action:@selector(onclickList)];
//        self.navigationItem.rightBarButtonItem=rightItem;
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.header beginRefreshing];
}

- (void)getData{
    WS(weakSelf);
    if (self.type==FansTypeDefault) {
        [[MyVM manage] getMyFansWithLastId:self.lastId Success:^(id returnValue) {
            NSArray *arr=returnValue;
            if (arr.count<[defaultPageSize integerValue]) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer endRefreshing];
            }
            [weakSelf.dataArr addObjectsFromArray:returnValue];
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView reloadData];
        } andFail:^(NSString *errorMessage, NSError *error) {
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView.footer endRefreshing];
        }];
    }else{
        [[MyVM manage] getMyFollowWithLastId:self.lastId Success:^(id returnValue) {
            NSArray *arr=returnValue;
            if (arr.count<[defaultPageSize integerValue]) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer endRefreshing];
            }
            [weakSelf.dataArr addObjectsFromArray:returnValue];
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView reloadData];
        } andFail:^(NSString *errorMessage, NSError *error) {
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView.footer endRefreshing];
        }];
    }
}

#pragma mark -- 取消关注
- (void)cancleFouse:(FouseModel *)model{
    WS(weakSelf);
    [[MyVM manage] cancleFollowWithId:model.FollowedUserId Success:^(id returnValue) {
        [SVProgressHUD showSuccessWithStatus:@"取消成功"];
        model.Status=0;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark -- 关注
- (void)addFouse:(FouseModel *)model{
    WS(weakSelf);
    [[MyVM manage] addFollowWithId:model.FollowedUserId Success:^(id returnValue) {
        [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        model.Status=1;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark --排行榜
- (void)onclickList{
    MasterRankVC  *vc=[[MasterRankVC alloc]init];
    vc.type=RankTypeFans;
    vc.title=@"粉丝榜";
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 76;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FansCell *cell=[tableView dequeueReusableCellWithIdentifier:@"FansCell" forIndexPath:indexPath];
    WS(weakSelf);
    cell.fouseModel=self.dataArr[indexPath.row];
    cell.focusBlock = ^(FouseModel *model) {
        if (model.Status==0) {
            [weakSelf addFouse:model];
        }else{
            [weakSelf cancleFouse:model];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OtherInfoVC *vc=[[OtherInfoVC alloc]init];
    FouseModel *model=self.dataArr[indexPath.row];
    if (self.type==FansTypeDefault) {
        vc.userId=model.UserId;
    }else{
        vc.userId=model.FollowedUserId;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
        headView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.tableHeaderView=headView;
        [_tableView registerClass:[FansCell class] forCellReuseIdentifier:@"FansCell"];
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            weakSelf.lastId=@"0";
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            FouseModel *model=weakSelf.dataArr.lastObject;
            weakSelf.lastId=model.Id;
            [weakSelf getData];
        }];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.
            insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
